package com.km.web;

import com.km.config.Config;
import com.km.entity.Article;
import com.km.entity.ArticleTags;
import com.km.entity.Tags;
import com.km.repository.ArticleRepository;
import com.km.repository.ArticleTagsRepository;
import com.km.repository.TagsRepository;
import com.km.service.ArticleService;
import com.km.util.DateUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>IndexController</p>
 * Created by zhezhiyong@163.com on 2017/9/20.
 */
@Controller
public class IndexController {

    @Resource
    private ArticleService articleService;
    @Resource
    private ArticleRepository articleRepository;
    @Resource
    private TagsRepository tagsRepository;
    @Resource
    private ArticleTagsRepository articleTagsRepository;
    @Resource
    private Config config;

    @RequestMapping("/")
    public String index(Model model) {
        Page<Article> articlePage = articleService.findAll(0, config.getSize());
        model.addAttribute("articlePage", articlePage);
        model.addAttribute("mainPage", "article/list");
        model.addAttribute("mainPageKey", "#f");
        return "index";
    }

    @RequestMapping("/page/{page}")
    public String page(@PathVariable Integer page, Model model) {
        if (page == null || page == 0) return "redirect:/";
        Page<Article> articlePage = articleService.findAll(page, config.getSize());
        model.addAttribute("articlePage", articlePage);
        model.addAttribute("mainPage", "article/list");
        model.addAttribute("mainPageKey", "#f");
        return "index";
    }

    @RequestMapping("/article/{id}")
    public String view(@PathVariable Long id, Model model) {
        Article article = articleRepository.findOne(id);
        Article prevArticle = articleRepository.findOne(id - 1);
        Article nextArticle = articleRepository.findOne(id + 1);
        article.setCreateDate(DateUtil.dateToString(article.getCreateTime()));
        model.addAttribute("article", article);
        if (prevArticle != null) {
            model.addAttribute("prev", true);
            model.addAttribute("prevUrl", "/article/" + prevArticle.getId());
            model.addAttribute("prevTitle", "<i class=\"fa fa-angle-left fa-fw\"></i>" + prevArticle.getTitle());
        }
        if (nextArticle != null) {
            model.addAttribute("next", true);
            model.addAttribute("nextUrl", "/article/" + nextArticle.getId());
            model.addAttribute("nextTitle", nextArticle.getTitle() + "<i class=\"fa fa-angle-right fa-fw\"></i>");
        }
        model.addAttribute("mainPage", "article/view");
        model.addAttribute("mainPageKey", "#f");
        return "index";
    }

    @RequestMapping("/archives")
    public String archives(Model model) {
        List<Article> articleList = articleRepository.findAll(new Sort(Sort.Direction.DESC, "id"));
        articleList.forEach(article -> article.setCreateDate(DateUtil.dateToString(article.getCreateTime())));
        model.addAttribute("articleList", articleList);
        model.addAttribute("mainPage", "article/archives");
        model.addAttribute("mainPageKey", "#f");
        return "index";
    }

    @RequestMapping("/about")
    public String about(Model model) {
        model.addAttribute("mainPage", "about");
        model.addAttribute("mainPageKey", "#f");
        return "index";
    }

    @RequestMapping("/music")
    public String music(Model model) {
        model.addAttribute("mainPage", "article/archives");
        model.addAttribute("mainPageKey", "#d");
        return "index";
    }

    @RequestMapping("/movie")
    public String movie(Model model) {
        model.addAttribute("mainPage", "article/archives");
        model.addAttribute("mainPageKey", "#d");
        return "index";
    }

    @RequestMapping("/link/{url}")
    public String link(@PathVariable String url, Model model) {
        List<Article> articleList= new ArrayList<>();
        Tags tags = tagsRepository.findTagsByUrl(url);
        if (tags != null){
            List<ArticleTags> articleTagsList = articleTagsRepository.findByTagsId(tags.getId());
            articleTagsList.forEach(articleTags -> {
                Article article = articleRepository.findOne(articleTags.getArticleId());
                article.setCreateDate(DateUtil.dateToString(article.getCreateTime()));
                articleList.add(article);
            });
        }
        model.addAttribute("articleList", articleList);
        model.addAttribute("mainPage", "article/tags");
        model.addAttribute("mainPageKey", "#f");
        return "index";
    }


    @RequestMapping("/loginError")
    public String loginError(Model model) {
        model.addAttribute("loginError", true);
        return "login";
    }


}
